﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Irandeb4.Models;

namespace Irandeb4.Controllers
{ 
    public class CustomerRequestController : Controller
    {
        private IrandebEntities db = new IrandebEntities();

        //
        // GET: /CustomerRequest/

        public ViewResult Index()
        {
            var orders = db.Orders.Include(o => o.Company);
            return View(orders.ToList());
        }

        //
        // GET: /CustomerRequest/Details/5

        public ViewResult Details(long id)
        {
            Order order = db.Orders.Find(id);
            return View(order);
        }

        //
        // GET: /CustomerRequest/Create

        public ActionResult Create()
        {
            IEnumerable<Company> companies = GetCustomers();
            ViewBag.CompanyId = new SelectList(companies, "CompanyId", "Name");
            ViewBag.Product = db.Products.ToList();
            //ViewBag.Items = new List<Item>();
            
            Order order = new Order();
            order.Items = new List<Item>();
            Session["ListaDeItems"] = order.Items;
            return View(order);
        } 

        //
        // POST: /CustomerRequest/Create

        [HttpPost]
        public ActionResult Create(Order order,string action)
        {
            IEnumerable<Company> companies = GetCustomers();
            if (ModelState.IsValid)
            {
                if (action == "Agregar")
                {
                    List<Item> items = (List<Item>)Session["ListaDeItems"];
                    order.Items = items;
                    //List<Item> addedItems = ViewBag.Items;

                    //addedItems.Add(order.ItemToSave);
                    //ViewBag.Items = addedItems;
                    Item newItem = order.ItemToSave;
                    order.Items.Add(newItem);
                    order.ItemToSave = new Item();
                    //items.Add(order.ItemToSave);
                    Session["ListaDeItems"] = order.Items;

                    ViewBag.CompanyId = new SelectList(companies, "CompanyId", "Name");
                    ViewBag.Product = db.Products.ToList();
                    //Session["ListaDeItems"] = items;
                    return View(order);
                }
                else
                {
                    db.Orders.Add(order);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            ViewBag.Product = db.Products.ToList();
            ViewBag.CompanyId = new SelectList(companies, "CompanyId", "Name"); ;
            return View(order);
        }

        private IEnumerable<Company> GetCustomers()
        {
            IEnumerable<Company> companies = from c in db.companies
                                             where c.Coustomer == true
                                             select c;
            return companies;
        }
        
        //
        // GET: /CustomerRequest/Edit/5
 
        public ActionResult Edit(long id)
        {
            Order order = db.Orders.Find(id);
            ViewBag.CompanyId = new SelectList(db.companies, "CompanyId", "Nit", order.CompanyId);
            return View(order);
        }

        //
        // POST: /CustomerRequest/Edit/5

        [HttpPost]
        public ActionResult Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CompanyId = new SelectList(db.companies, "CompanyId", "Nit", order.CompanyId);
            return View(order);
        }

        //
        // GET: /CustomerRequest/Delete/5
 

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}